package com.crm.controller.repositoryController;

import com.alibaba.druid.util.StringUtils;
import com.crm.dao.purchaseDAO.PurchaseproductiondetailDAO;
import com.crm.exception.ServiceException;
import com.crm.pojo.*;
import com.crm.service.permission.UserInfoService;
import com.crm.service.purchaseService.PurchaseService;
import com.crm.service.purchaseService.PurchasergService;
import com.crm.service.purchaseService.PurproductDetailsService;
import com.crm.service.repositoryService.IOdetailsService;
import com.crm.service.repositoryService.IOlogService;
import com.crm.service.repositoryService.RepositoryService;
import com.crm.service.saling.*;
import com.crm.service.saling.impl.ContractServiceImpl;
import com.crm.service.saling.impl.DispatcherDetailServiceImpl;
import com.crm.service.saling.impl.DispatcherServiceImpl;
import com.crm.vo.CODetailsVO;
import com.crm.vo.IOdetailVO;
import com.crm.vo.Result;
import org.hibernate.annotations.AttributeAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class InAndOutlogController {
    @Autowired
    private IOlogService iOlogService;
    @Autowired
    private DispatcherService dispatcherService;
    @Autowired
    private DispatcherDetailService dispatcherDetailService;
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private IOdetailsService iOdetailsService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RefundService refundService;
    @Autowired
    private PurchasergService purchasergService;
    @Autowired
    private RefundDetailService refundDetailService;
    @Autowired
    private PurproductDetailsService purproductDetailsService;
    @Autowired
    private CODetailsService coDetailsService;
    //打开出入库选项卡
    @GetMapping("pIOR")
    public String toPIOR(){
        return "repository/proIOR";
    }

    //打开新建出入库流水表单
    @GetMapping("to-form-IO-add")
    public String toFormIOadd(){
        return "repository/newIOlog";
    }
    //提交出入库表单
    @PostMapping("newIOlog-form-sub")
    @ResponseBody
    public Result newIOlog(Inandoutlog inandoutlog){
        try{
            if(inandoutlog.getIotype()!=0){
                inandoutlog.setInandoutlogIsDel("否");
                if(inandoutlog.getIotype()==1){
                    Dispatcher dispatcher = dispatcherService.findOneById(inandoutlog.getIoDispatherId());
                    dispatcher.setDispatcherState("已发货");
                    dispatcherService.saveDispatcher(dispatcher);
                    List<CODetailsVO> list = coDetailsService.showOrderDetailsList(dispatcher.getFkOrderId().longValue());
                    List<DispatcherDetail> dlist = dispatcherDetailService.findAllBYdid(inandoutlog.getIoDispatherId());
                    List<CODetail> clist = new ArrayList<>();
                    for (CODetailsVO vo:list) {
                        for(DispatcherDetail d:dlist){
                            if(vo.getCoDetail().getCOProductId().longValue()==d.getPId().longValue()){
                                vo.getCoDetail().setFkOrderId(vo.getCoDetail().getFkOrderId().subtract(d.getProductAccount()));
                                vo.getCoDetail().setProductType((String.valueOf(Long.parseLong(vo.getCoDetail().getProductType())+d.getProductAccount().longValue())));
                            }
                        }
                        clist.add(vo.getCoDetail());
                    }
                    coDetailsService.saveCoDetailList(clist);
                    dispatcherDetailService.toCheck(inandoutlog.getIoDispatherId());
                }if(inandoutlog.getIotype()==2){
                    Purchase purchase =purchaseService.findOneById(inandoutlog.getIoDispatherId());
                    purchase.setPurstatus("采购完成-已入库");
                    purchaseService.savePurchase(purchase);
                }if(inandoutlog.getIotype()==3){
                    Refund refund = refundService.findOneById(inandoutlog.getIoDispatherId());
                    refund.setRefundState("已收货");
                    refundService.saveARefund(refund);
                    refundDetailService.toCheck(inandoutlog.getIoDispatherId());
                }if(inandoutlog.getIotype()==4){
                     Purchasergdetail purchasergdetail = purchasergService.findOneById(inandoutlog.getIoDispatherId());
                     purproductDetailsService.setrg2(purproductDetailsService.findOne(purchasergdetail.getPpurdid()));
                     purchasergdetail.setPurchasergdetailsIsDel("退货完成-已出库");
                     purchasergService.save(purchasergdetail);
                }
                iOlogService.addIOlog(inandoutlog);
            }else{
                inandoutlog.setInandoutlogIsDel("是");
            }
            return Result.SUCCESS;
        }catch(ServiceException e){
            e.printStackTrace();
            return new Result("添加失败！");
        }
    }
    //模糊查询出库单或者采购入库单
    @PostMapping("getAllpurOrorderBymh")
    @ResponseBody
    public List<Inandoutlog> findAllpurOrorderBymh(String q, String iotype){
        List<Inandoutlog> list  =new ArrayList<>();
            if(!StringUtils.isEmpty(iotype)) {
                if ("1".equals(iotype)) {
                    if (!StringUtils.isEmpty(q.trim())) {
                        list = dispatcherService.findAllOrderBymh(q.trim());
                    } else {
                        list = dispatcherService.findAllOrderBymh2();
                    }
                }
                if ("2".equals(iotype)) {
                    if (!StringUtils.isEmpty(q.trim())) {
                        list = purchaseService.findAllBymh(q.trim());
                    } else {
                        list = purchaseService.findAllBymh2();
                    }
                }
                if ("3".equals(iotype)) {
                    list = refundService.findAllBymh();
                }
                if ("4".equals(iotype)) {
                    list = purchasergService.findAllBymh();
                }
            }
        return list;
    }
    //模糊查询经手人通过经手人名称或昵称
    @PostMapping("findUserBypiny")
    @ResponseBody
    public List<Inandoutlog> findUserBypiny(String q){
        List<Inandoutlog> list = new ArrayList<>();
        if(!StringUtils.isEmpty(q)){
            list = userInfoService.findAllUsersBymh(q);
        }
        return list;
    }
    //分页查询所有的出入库流水
    @PostMapping("allIOlogPaged")
    @ResponseBody
    public Map allProPaged(int page, int rows) {
        Long total = iOlogService.queryCount();
        List<Inandoutlog> io = iOlogService.allIOlogPaged(page, rows);
        HashMap map = new HashMap<>();
        map.put("total", total);
        map.put("rows", io);
        return map;
    }
    //模糊查询标题或仓库名称
    @PostMapping("queryByioidOriotitleMh")
    @ResponseBody
    public List<Inandoutlog> queryByioidOriotitleMh(String q,String b){
        List<Inandoutlog> list = new ArrayList<>();
        try{
            //查询所有内容
            if("i1".equals(b)){
                list = iOlogService.findAllIOByrnameOrtitle(q);
            }else if("i2".equals(b)){
                list = iOlogService.findAllIOByrnameOrtitle2(q);
            }else if("i3".equals(b)){
                list =iOlogService.findAllIOByrnameOrtitle3(q);
            }else {
            }
        }catch (ServiceException e){
            e.printStackTrace();
        }
        return list;
    }
    //通过ajax请求获取对应的ioid和iotype的集合 并分页
    @PostMapping("getDetailsData")
    @ResponseBody
    public Map getDetailsData(Long ioid,Long iotype) {
        HashMap map = new HashMap<>();
        if(iotype!=null&&ioid!=null){
            //出库
            if(iotype==1){
                Long total = iOdetailsService.queryCountD(ioid);
                List<IOdetailVO> vo = iOdetailsService.findAllIOdetailD(ioid);
                map.put("total", total);
                map.put("rows", vo);
            }
            //入库
            else if(iotype==2){
                Long total = iOdetailsService.queryCountP(ioid);
                List<IOdetailVO> vo = iOdetailsService.findAllIOdetailP(ioid);
                map.put("total", total);
                map.put("rows", vo);
            //退货入库
            }else if(iotype==3){
                Long total =iOdetailsService.queryCountIn(ioid);
                List<IOdetailVO> vo =iOdetailsService.findAllIOdetailIn(ioid);
                map.put("total",total);
                map.put("rows",vo);
            //退货出库
            }else if(iotype==4){
                Long total =iOdetailsService.queryCountOut(ioid);
                List<IOdetailVO> vo =iOdetailsService.findAllIOdetailOut(ioid);
                map.put("total",total);
                map.put("rows",vo);
            }else{

            }
        }
        return map;
    }
    //查询所有存在的仓库 根据仓库名称排序
    @PostMapping("findAllR")
    @ResponseBody
    public List<Repository> findAllR(){
        return repositoryService.findAllR();
    }
}
